package cn.ibizlab.odoo.core.odoo_digest.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Idigest_tip;
import cn.ibizlab.odoo.core.odoo_digest.domain.Digest_tip;
import cn.ibizlab.odoo.core.odoo_digest.filter.Digest_tipSearchContext;
import cn.ibizlab.odoo.core.odoo_digest.service.IDigest_tipService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[摘要提示] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Digest_tipServiceImpl implements IDigest_tipService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Idigest_tipClientService digest_tipClientService;

    @Override
    public void updateBatch(List<Digest_tip> digest_tips, int batchSize) {

        //digest_tipClientService.updateBatch(digest_tips,batchSize) ;

    }

    @Override
    public boolean create(Digest_tip digest_tip) {
        boolean bOk = false ;
        Idigest_tip clientModel = this.convert2Model(digest_tip,null);
        digest_tipClientService.create(clientModel) ;
        this.convert2Domain(clientModel,digest_tip);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void createBatch(List<Digest_tip> digest_tips, int batchSize) {

                        //digest_tipClientService.createBatch(digest_tips,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Idigest_tip clientModel = digest_tipClientService.createModel();
        clientModel.setId(id);
        digest_tipClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //digest_tipClientService.removeBatch(digest_tips) ;

    }

    @Override
    public boolean update(Digest_tip digest_tip) {
        boolean bOk = false ;
        Idigest_tip clientModel = this.convert2Model(digest_tip,null);
        digest_tipClientService.update(clientModel) ;
        this.convert2Domain(clientModel,digest_tip) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public Digest_tip get(Integer id) {
        Idigest_tip clientModel = digest_tipClientService.createModel();
        clientModel.setId(id);
        digest_tipClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Digest_tip get(Digest_tip digest_tip) {
        Digest_tip retdigest_tip = this.get(digest_tip.getId()) ;
    	BeanCopier copier=BeanCopier.create(retdigest_tip.getClass(), digest_tip.getClass(), false);
        copier.copy(retdigest_tip, digest_tip, null);
        return digest_tip ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Digest_tip> searchDefault(Digest_tipSearchContext context) {
        List<Digest_tip> list = new ArrayList<Digest_tip>();
        Page<cn.ibizlab.odoo.core.client.model.Idigest_tip> clientModelList = digest_tipClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Idigest_tip clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Digest_tip>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据访问群查询
     */
    @Override
    public Page<Digest_tip> selectByOdoo_group(Integer id){
        Digest_tipSearchContext context = new Digest_tipSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Digest_tip> list = new ArrayList<Digest_tip>();
        Page<cn.ibizlab.odoo.core.client.model.Idigest_tip> clientModelList = digest_tipClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Idigest_tip clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Digest_tip>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Digest_tip> selectByOdoo_create(Integer id){
        Digest_tipSearchContext context = new Digest_tipSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Digest_tip> list = new ArrayList<Digest_tip>();
        Page<cn.ibizlab.odoo.core.client.model.Idigest_tip> clientModelList = digest_tipClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Idigest_tip clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Digest_tip>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Digest_tip> selectByOdoo_write(Integer id){
        Digest_tipSearchContext context = new Digest_tipSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Digest_tip> list = new ArrayList<Digest_tip>();
        Page<cn.ibizlab.odoo.core.client.model.Idigest_tip> clientModelList = digest_tipClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Idigest_tip clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Digest_tip>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Idigest_tip convert2Model(Digest_tip domain , Idigest_tip model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = digest_tipClientService.createModel() ;
        }

         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getTip_descriptionDirtyFlag())
            model.setTip_description(domain.getTip_description());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getUser_idsDirtyFlag())
            model.setUser_ids(domain.getUser_ids());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getGroup_id_textDirtyFlag())
            model.setGroup_id_text(domain.getGroup_id_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getGroup_idDirtyFlag())
            model.setGroup_id(domain.getGroup_id());
        return model ;
    }

    public Digest_tip convert2Domain( Idigest_tip model ,Digest_tip domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Digest_tip();
        }

        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getTip_descriptionDirtyFlag())
            domain.setTip_description(model.getTip_description());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getUser_idsDirtyFlag())
            domain.setUser_ids(model.getUser_ids());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getGroup_id_textDirtyFlag())
            domain.setGroup_id_text(model.getGroup_id_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getGroup_idDirtyFlag())
            domain.setGroup_id(model.getGroup_id());
        return domain ;
    }


}
